

#######################################################################
# Replication material for "The Social Dynamics of Collective Action: #
# Evidence from the Diffusion of the Swing Riots, 1830-31"            #
#                                                                     #
# Aidt, Leon and Satchell (2020)                                      #
#######################################################################



rm(list=ls())

library(spdep)
library(foreign)

# Stata file saved in old version (12 or before)
riots <- read.dta("Swing_data_LT.dta")


# THE FOLLOWING CREATES THE MATRIX OF DISTANCES
# Create a matrix of locations
loc<-cbind(riots$longitude,riots$latitude)
# Find all neighbours within a 10 km radius (Great Circle distances)
nearby<-dnearneigh(loc,0,10,longlat=TRUE)
# Create the weight matrix (binary)
W<-nb2mat(nearby,glist=NULL,style="B",zero.policy=TRUE)


# Create a diagonal matrix of the 'correct' dimension
dim(W)
I<-diag(10309)


######################
## Long-term Impact ##
######################


# Create the augmented weight matrix that we then raise to different powers (the coefficient is from the first column of
# table 1)

Delta<-(0.0029*W +0.032*I)
M1<-Delta
M2<-M1+Delta^2
M3<-M2+Delta^3
M4<-M3+Delta^4
M5<-M4+Delta^5
M6<-M5+Delta^6
M7<-M6+Delta^7
M8<-M7+Delta^8
M9<-M8+Delta^9
M10<-M9+Delta^10

# Sum columns and calculate the summary statistics
impact1<-matrix(colSums(M1))
impact2<-matrix(colSums(M2))
impact3<-matrix(colSums(M3))
impact4<-matrix(colSums(M4))
impact5<-matrix(colSums(M5))
impact6<-matrix(colSums(M6))
impact7<-matrix(colSums(M7))
impact8<-matrix(colSums(M8))
impact9<-matrix(colSums(M9))
impact10<-matrix(colSums(M10))

# Calculate the mean and standard deviation
mean(impact1)
median(impact1)
sd(impact1)
min(impact1)
max(impact1)

mean(impact2)
sd(impact2)

mean(impact3)
sd(impact3)

mean(impact4)
sd(impact4)

mean(impact5)
sd(impact5)

mean(impact6)
sd(impact6)

mean(impact7)
sd(impact7)

mean(impact8)
sd(impact8)

mean(impact9)
sd(impact9)

mean(impact10)
sd(impact10)
min(impact10)
max(impact10)
fifth_pctile <- quantile(impact10, c(.05))


time = matrix(c(0,1,2,3,4,5,6,7,8,9,10), nrow=11, ncol=1)
means = matrix(c(0, mean(impact1), mean(impact2), mean(impact3), mean(impact4), mean(impact5), mean(impact6), mean(impact7), mean(impact8), mean(impact9), mean(impact10)), nrow=11, ncol=1) 
two_five_pctile = matrix(c(0, quantile(impact1, c(.025)), quantile(impact2, c(.025)), quantile(impact3, c(.025)), quantile(impact4, c(.025)), quantile(impact5, c(.025)), quantile(impact6, c(.025)), quantile(impact7, c(.025)), quantile(impact8, c(.025)), quantile(impact9, c(.025)), quantile(impact10, c(.025))), nrow=11, ncol=1)
ninety_seven_five_pctile = matrix(c(0, quantile(impact1, c(.975)), quantile(impact2, c(.975)), quantile(impact3, c(.975)), quantile(impact4, c(.975)), quantile(impact5, c(.975)), quantile(impact6, c(.975)), quantile(impact7, c(.975)), quantile(impact8, c(.975)), quantile(impact9, c(.975)), quantile(impact10, c(.975))), nrow=11, ncol=1)

dat<-cbind(means, two_five_pctile, ninety_seven_five_pctile) 

plot(dat[,1], type="b", col="black", xlim=c(1,10), ylim=c(0,0.3), lwd=1, pch=15, xlab="time", ylab="impact after time t")
lines(dat[,2], type="l", col="black", lwd=0.3)
lines(dat[,3], type="l", col="black", lwd=0.3)



